package com.xhg.mqtt.util;

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 一些操作工具
 *
 * @author E.T
 * @version 1.0
 * @Description
 * @date 2018-07-26 15:05
 * @updateby
 * @updatedate
 * @since 1.0
 */
public class CommonUtil {

  /**
   * 将整型数字转换为二进制字符串，一共8位，不舍弃前面的0
   *
   * @param number 整型数字
   * @return 二进制字符串
   */
  public static String get8BitBinString(int number) {
    StringBuilder sBuilder = new StringBuilder();
    for (int i = 0; i < 8; i++) {
      sBuilder.append(number & 1);
      number = number >>> 1;
    }
    return sBuilder.reverse().toString();
  }

  /**
   * 是否需要ack回复
   */
  public static boolean isAck(int cc) {
    int val = Integer.valueOf(String.valueOf(cc), 2);
    return val == 1;
  }

  /**
   * 将二进制转成int
   */
  public static int getBinToInt(int cc) {
    return Integer.valueOf(String.valueOf(cc), 2);
  }

  /**
   * 获取uuid
   */
  public static String getUUID(String uuid) {
    String s = "";
    if ("".equals(uuid)) {
      s = UUID.randomUUID().toString().replace("-", "");
    } else {
      s = uuid;
    }
    s = s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
    return s.toUpperCase();
  }

  /**
   * 替换topic中的占位符
   *
   * @param topic topic
   * @param map 占位符列表
   */
  public static String replaceTopic(String topic, Map<String, String> map) {
    List<String> doc = extractMessageByRegular(topic);
    //获取占位符内容
    for (String key : doc) {
      String value = map.get(key);
      if (value != null) {
        topic = topic.replace(key, value);
      }
    }
    return topic;
  }

  /**
   * 替换topic中的占位符
   *
   * @param topicsList topic列表
   * @param map 占位符列表
   */
  public static String[] replaceTopics(List<String> topicsList, Map<String, String> map) {
    String[] topics = null;
    if (topicsList != null && topicsList.size() > 0) {
      topics = new String[topicsList.size()];
      for (int i = 0; i < topicsList.size(); i++) {
        String topic = topicsList.get(i);
        topics[i] = topic;
        List<String> doc = extractMessageByRegular(topic);
        //获取占位符内容
        for (String key : doc) {
          String value = map.get(key);
          if (value != null) {
            topic = topic.replace(key, value);
            topics[i] = topic;
          }
        }
      }
    }
    return topics;
  }

  /**
   * 使用正则表达式提取括号中的内容
   */
  public static List<String> extractMessageByRegular(String msg) {
    List<String> list = new ArrayList<String>();
    String regex = "(?<=\\{)(.+?)(?=\\})";//去掉{}
    String express = "(\\{\\w+\\})";//包含{}
    Pattern p = Pattern.compile(express);
    Matcher m = p.matcher(msg);
    while (m.find()) {
      list.add(m.group());
    }
    return list;
  }

  /**
   * 获取3-10随机数
   * @return
   */
  public static int getRandomTime() {
    int min = 3;
    int max = 10;
    Random random = new Random();
    int s = random.nextInt(max) % (max - min + 1) + min;
    return s;
  }

  /**
   * @Description:将Throwable对象的错误堆栈内容形成字符串<br>
   */
  public static String getThrowableStrRep(Throwable throwable) {
    if (throwable == null) {
      return null;
    }
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    throwable.printStackTrace(pw);
    pw.flush();
    LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString()));
    ArrayList lines = new ArrayList();
    try {
      String line = reader.readLine();
      while (line != null) {
        lines.add(line);
        line = reader.readLine();
      }
    } catch (IOException ex) {
      lines.add(ex.toString());
    }
    String[] rep = new String[lines.size()];
    lines.toArray(rep);
    return Arrays.toString(rep);
  }

}
